package com.honghu.paylibrary;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.honghu.paylibrary.bean.PayTypeBean;

import java.util.List;

import io.reactivex.annotations.NonNull;


/**
 * 创建日期 2021/4/19
 * 描述：
 */
public class PayAdapter extends RecyclerView.Adapter<PayAdapter.PayViewHolde> {

    private Context mContext;
    private List<PayTypeBean> mList;

    public PayAdapter(Context context, List<PayTypeBean> list) {
        mContext = context;
        mList = list;
    }

    @NonNull
    @Override
    public PayViewHolde onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_dialog_fragment_pay,null);
        PayViewHolde payViewHolde = new PayViewHolde(view);
        return payViewHolde;
    }

    @Override
    public void onBindViewHolder(@NonNull PayViewHolde holder, int position) {
        final PayTypeBean payTypeBean = mList.get(position);
        holder.content.setText(payTypeBean.getValue().getDesc());
        holder.icon.setImageResource(payTypeBean.getValue().getSrc());
        holder.ivSelect.setSelected(payTypeBean.isSelect());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (PayTypeBean typeBean : mList) {
                    typeBean.setSelect(false);
                    Log.d("ddddddddddd", "onClick: "+typeBean.getValue().getDesc());
                }
                payTypeBean.setSelect(true);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onBindViewHolder(@NonNull PayViewHolde holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
       /* if (payloads.size()>0&&payloads.get(0) == 1){
            PayTypeBean payTypeBean = mList.get(position);
            holder.ivSelect.setSelected(payTypeBean.isSelect());
        }*/
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class PayViewHolde extends RecyclerView.ViewHolder {

        private ImageView icon,ivSelect;
        private TextView content;
        public PayViewHolde(@NonNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.iv_icon);
            content = itemView.findViewById(R.id.tv_name);
            ivSelect = itemView.findViewById(R.id.iv_selected);
        }
    }
}
